<template>
	<div
			style="display:flex;justify-content: center;align-items: center;flex-direction: column;background: transparent;height: 1000px; margin-top: -260px">
		<div
				style="display: flex; align-items: center; justify-content: center; flex-direction: column; height: 100vh; background-color: rgba(247,247,247,0); text-align: center;">
			<h1 style="margin-bottom: 30px; font-family: 'Arial', sans-serif; color: #f80000;">
				<img src="/广州软件学院.png" alt="广州软件学院" style=" height: auto;">
			</h1>
			<h1 style="margin-bottom: 30px; font-family: '华文仿宋', sans-serif ;font-size:50px;background: -webkit-linear-gradient(315deg,#00bbff 25%,#0024ff);-webkit-background-clip: text;
      -webkit-text-fill-color: transparent;color:transparent;font-weight:bolder; text-shadow: 6px 4px 8px rgba(0, 0, 0, 0.3);">
				"无忧综测"<br>综合综测服务管理平台
			</h1>
		</div>
		<div class="container login_Image" style="margin-top: -260px">
			<div class="container-form container-signup"
					 style="margin-left: 50%;width: 250px;height:500px;padding-right:15px ">
				<div>
					<form @submit.prevent="resetPassword" class="form">
						<div style="height: 300px; max-width: 600px;transform: scale(0.7);margin-top: -30px;margin-left: -100px">
							<el-steps direction="vertical" :active="activeStep">
								<el-step title="输入学号" :key="1"/>
								<el-step title="回答密保问题" :key="2"/>
								<el-step title="重置密码" :key="3"/>
							</el-steps>
						</div>
						<div style="width: 300px;margin-left: -100px;margin-top: -60px">
							<div v-if="activeStep === 1" class="step-content">
								<el-form>
									<el-form-item label="账号">
										<el-input
												class="inputBox"
												v-model="ResetPassword.StudentID"
												style="width: 240px"
												required placeholder="请输入您的账号"
										/>
									</el-form-item>
									<el-button @click="nextStep" class="button22"
														 style="width: 100px;font-size: 20px;margin-top: 58px;margin-left: 80px">
										下一步
									</el-button>
								</el-form>
							</div>
							<div v-if="activeStep === 2" class="step-content">
								<el-form>
									<el-form-item label="问题">
										<el-input
												class="inputBox"
												v-model="ResetPassword.question"
												style="width: 240px"
												disabled
										/>
									</el-form-item>
									<el-form-item label="答案">
										<div class="inputBox">
											<input type="text" v-model="ResetPassword.answer" required placeholder="请输入您的答案">
										</div>
									</el-form-item>
									<el-button @click="prevStep" class="button22"
														 style=";width: 100px;font-size: 20px;margin-left: -20px">
										上一步
									</el-button>
									<el-button @click="validateAnswer" class="button22" style="margin: 20px;width: 100px;font-size: 20px">
										下一步
									</el-button>
								</el-form>
							</div>
							<div v-if="activeStep === 3" class="step-content">
								<el-form>
									<el-form-item label="密码">
										<div class="inputBox" style="margin-left:12px">
											<input type="password" v-model="ResetPassword.password" required>
										</div>
									</el-form-item>
									<el-form-item label="确认密码" style="margin-left:-15px">
										<div class="inputBox" style="margin-left:0">
											<input type="password" v-model="ResetPassword.confirmPassword" required>
										</div>
									</el-form-item>
									<el-button @click="prevStep" class="button22"
														 style=";width: 100px;font-size: 20px;margin-left: -20px">
										上一步
									</el-button>
									<el-button type="primary" @click="submitResetPassword" class="button22"
														 style="margin: 20px;width: 100px;font-size: 20px">提交
									</el-button>
								</el-form>
							</div>
						</div>
						<div style="display: flex;justify-content: space-between">
						</div>
						<br>
						<br>
						<br></form>
				</div>
			</div>
			<!-- 登录 -->
			<div class="container-form container-signIn" :class="{ 'panel-active': !signUpActive }">
				<div>
					<div class="login-app">
						<input type="radio" name="opt" id="uname">
						<label for="uname" class="u-btn" style="width: 100%">用户登录</label>
						<div id="form-bar" style="margin-top: -45px">
							<form @submit.prevent="accountLogin" class="u-form">
								<div class="inputBox">
									<input type="text" v-model="loginForm.StudentID" required>
									<span>账号/ID</span>
								</div>
								<br>
								<div class="inputBox">
									<input type="password" v-model="loginForm.password" required>
									<span>密码/Password</span>
								</div>
								<div class="inputBox" style="margin-top: 20px">
									<input type="password" v-model="loginForm.VerificationCodeInput" required>
									<span>验证码/VerificationCode</span>
								</div>
								<div>
									<img :src="captchaUrl" alt="验证码" @click="fetchCaptcha" style="margin: 20px 80px 0 0 "/>
								</div>
								<br>
								<button type="submit" class="button" style="font-size: 20px">登录</button>
								<br><br>
							</form>
						</div>
					</div>
				</div>
			</div>
			<div class="container" :class="{ 'panel-active': isSignUpActive }">
				<!-- Overlay Panels -->
				<div class="container-overlay">
					<div class="overlay">
						<div class="overlay-panel overlay-left">
							<button class="button" @click="togglePanel(false)" style="font-size: 20px">
								点击登录
							</button>
						</div>
						<div class="overlay-panel overlay-right">
							<button class="button" @click="togglePanel(true)" style="font-size: 20px">
								忘记密码?
							</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import {ElMessage} from 'element-plus';
import axiosService from '@/plugins/Axios.js';
import {ref} from 'vue';
import {mapState} from 'vuex';

export default {
	setup() {
		const activeStep = ref(1);
		const account = ref('');
		const password = ref('');
		const Code = ref('');

		const nextStep = () => {
			activeStep.value++;
		};

		const prevStep = () => {
			activeStep.value--;
		};

		const submit = () => {
			console.log('提交表单:', {
				account,
				password,
				Code
			});
		};
		return {
			activeStep,
			nextStep,
			prevStep,
			submit
		}
	},
	computed: {
		...mapState(['CurrentUser'])
	},
	data() {
		return {
			code: '',
			captchaUrl: null,
			isSignUpActive: false,
			signUpActive: false,
			currentId: '',
			ResetPassword: {
				StudentID: '',
				question: '',
				answer: '',
				password: '',
				confirmPassword: '',
			},
			loginForm: {
				StudentID: '',
				password: '',
				VerificationCodeInput: ''
			},
			lastValidated: 0,
			validateInterval: 3000,
		}
	},
	methods: {
		togglePanel(signUp) {
			this.isSignUpActive = signUp;
		},
		async fetchCaptcha() {
			this.captchaUrl = await axiosService.fetchCaptcha();
		},
		async accountLogin() {
			try {
				this.currentId = this.loginForm.StudentID;
				const response = await axiosService.accountLogin(this.loginForm.StudentID, this.loginForm.password, this.loginForm.VerificationCodeInput);
				if (response.code !== 0 && response.message === '登录成功') {
					// ElMessage.success(response.code);
					console.log(response.code);
					let redirectPath = '/';
					switch (response.code) {
						case 1:
							const response2 = await axiosService.fetchStudent(this.currentId);
							console.log("学生数据？"+response2.data);
							this.$store.commit('setCurrentUser', response2.data);
							ElMessage.success("学生系统登录成功")
							redirectPath = `/Student/${this.currentId}`;
							break;
						case 2:
							const response3 = await axiosService.fetchDepartment(this.currentId);
							console.log("部门数据？"+response3.data);
							this.$store.commit('setCurrentUser', response3.data);
							ElMessage.success("部门系统登录成功")
								try {
									const response3 = await axiosService.fetchDepartment(this.currentId);
									console.log(response3.data);
									this.$store.commit('setCurrentDepartment', response3.data);
								} catch (error) {
									console.error('查询部门失败:', error);
								}
							redirectPath = `/Community/${this.currentId}`;
							break;
						case 3:
							ElMessage.success("管理系统登录成功")
							redirectPath = `/Admin`;
							break;
					}
					this.$router['push'](redirectPath);
				} else {
					ElMessage.error("身份认证失败 请正确输入用户名、密码和验证码");
				}
			} catch (error) {
				console.error('登录请求失败:', error);
			}
		},
		async resetPassword() {
			try {
				const response = await axiosService.resetPassword(this.ResetPassword.password);
				if (response.code === 1) {
					ElMessage.success('密码重置成功，请登录');
					this.togglePanel(false);
				} else {
					ElMessage.error(response.msg);
				}
			} catch (error) {
				console.error('重置密码请求失败:', error);
			}
		},
		async validateAnswer() {
			const now = Date.now();
			if (now - this.lastValidated < this.validateInterval) {
				ElMessage.warning('操作太频繁，请3s后再试。');
				return;
			}
			this.lastValidated = now;
			try {
				const Validated = await axiosService.validateAnswer(this.ResetPassword.answer);
				if (Validated.message ==="检验成功") {
					await this.nextStep();
					ElMessage.info('验证成功');
				} else{
					ElMessage.error('回答错误！');
				}
			} catch (error) {
				console.error('验证密保问题请求失败:', error);
			}
		},
		async nextStep() {
			if (this.activeStep === 1) {
				try {
					const response = await axiosService.post("/logins/GetId?id=" + this.ResetPassword.StudentID);
					if (response.data.message === "检验成功") {
						const studentQuestionResponse = await axiosService.getStudentQuestion(this.ResetPassword.StudentID);
						if (studentQuestionResponse && studentQuestionResponse[0] && studentQuestionResponse[0].problem) {
							this.ResetPassword.question = studentQuestionResponse[0].problem;
							console.log('problem', this.ResetPassword.question);
							this.activeStep++;
						}
						const departmentQuestionResponse = await axiosService.getDepartmentQuestion(this.ResetPassword.StudentID);
						if (departmentQuestionResponse && departmentQuestionResponse[0] && departmentQuestionResponse[0].problem) {
							this.ResetPassword.question = departmentQuestionResponse[0].problem;
							console.log('problem', this.ResetPassword.question);
							this.activeStep++;
						}
					} else {
						ElMessage.error(response.data.message);
					}
				} catch (error) {
					console.error('在获取问题时发生错误:', error);
				}
			} else {
				this.activeStep++;
			}
		},
		prevStep() {
			this.activeStep--;
		},
		async submitResetPassword() {
			if (this.ResetPassword.password !== this.ResetPassword.confirmPassword) {
				ElMessage.error('两次输入的密码不一致');
				return;
			}
			try {
				const response = await axiosService.submitResetPassword(this.ResetPassword.password);
				if (response.code === 1 || response.code === 2) {
					ElMessage.success('密码重置成功');
					this.togglePanel(false);
				} else {
					ElMessage.error(response.msg);
				}
			} catch (error) {
				console.error('提交重置密码请求失败:', error);
			}
		}
	},
	mounted() {
		this.fetchCaptcha();
	}
};
</script>
<style scoped>

body {
	height: 250vh;
	display: flex;
	justify-content: center;
	align-items: center;
	background-color: #000;
}

button {
	position: relative;
	display: inline-block;
	width: 300px;
	height: 35px;
	justify-content: center;
	line-height: 5px;
	text-align: center;
	color: rgb(0, 0, 0);
	font-size: 30px;
	text-decoration: none;
	background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
	background-size: 400%;
	border-radius: 30px;
}

button:hover {
	animation: animate 8s linear infinite;
}

@keyframes animate {
	0% {
		background-position: 0 0;
	}
	100% {
		background-position: 400% 0;
	}
}


button:before {
	content: '';
	position: absolute;
	left: -5px;
	top: -5px;
	right: -5px;
	bottom: -5px;
	z-index: -1;
	background: transparent;
	background-size: 400%;
	border-radius: 10px;
	opacity: 0;
	transition: all 1s;
}

button:hover:before {
	filter: blur(20px);
	opacity: 1;
}

body {
	width: 100vw;
	height: 100vh;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	background: transparent;
}

label {
	font-family: "Gluten", serif;
	font-size: 128px;
	z-index: 1;
	color: #fff;
	margin: -50px 0 32px;
}

label::after {
	content: "%";
}


.inputBox {
	margin-left: 20px;
	position: relative;
	width: 250px;
}

.inputBox input {
	width: 100%;
	padding: 10px;
	border: 1px solid #ccc;
	border-radius: 5px;
	font-size: 1em;
	outline: none;
}

.inputBox span {
	position: absolute;
	left: 0;
	top: 0;
	padding: 10px;
	color: #ccc;
	font-size: 1em;
	pointer-events: none;
	transition: all 0.5s;
}

.inputBox input:valid ~ span,
.inputBox input:focus ~ span {
	color: orange;
	transform: translateX(10px) translateY(-10px);
	font-size: 0.8em;
	padding: 0 10px;
	background-color: #fff;
}

body {
	width: 100vw;
	height: 100vh;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	background: transparent;
}

label {
	font-family: "Gluten", serif;
	font-size: 128px;
	z-index: 1;
	color: #fff;
	margin: -50px 0 32px;
}

label::after {
	content: "";
}


/* 主体 div 样式 */
.container {
	background-color: #e7e7e7;
	border-radius: 0.7rem;
	box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25),
	0 0.7rem 0.7rem rgba(0, 0, 0, 0.22);
	height: 400px;
	max-width: 750px;
	overflow: hidden;
	position: relative;
	width: 100%;
}


/* 登录、注册框部分 */
.container-form {
	height: 100%;
	position: absolute;
	top: 0;
	transition: all 0.6s ease-in-out;
}

/* 登录框 - 默认层级高 */
.container-signIn {
	left: 0;
	width: 50%;
	z-index: 2;
}

/* 注册框 - 默认层级低 - 透明度 0 */
.container-signup {
	left: 0;
	opacity: 1;
	width: 50%;
	z-index: 3;
}


/* 表单样式 */
.form {
	background-color: #fdfdfd;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
	padding: 0 3rem;
	height: 100%;
	text-align: center;
}

.button {
	background-color: #00ffff;
	box-shadow: 0 4px 4px rgba(255, 112, 159, .3);
	border-radius: 5px;
	color: #e7e7e7;
	border: none;
	cursor: pointer;
	font-size: 0.8rem;
	font-weight: bold;
	letter-spacing: 0.1rem;
	padding: 0.9rem 4rem;
	text-transform: uppercase;
	transition: transform 80ms ease-in;
}

.button22 {
	background-color: #00ffff;
	box-shadow: 0 4px 4px rgba(255, 112, 159, .3);
	border-radius: 5px;
	color: #e7e7e7;
	border: none;
	cursor: pointer;

	font-weight: bold;
	text-transform: uppercase;
	transition: transform 80ms ease-in;
}

.form > .button {
	margin-top: 2.0rem;
}

.button:active {
	transform: scale(0.95);
}

/* ---------- 叠加部分样式 ------------- */
.container-overlay {
	height: 100%;
	left: 50%;
	overflow: hidden;
	position: absolute;
	top: 0;
	transition: transform 0.6s ease-in-out;
	width: 50%;
	z-index: 100;
}

.overlay {
	width: 200%;
	height: 100%;
	position: relative;
	left: -100%;
	background: #292929;
	background-size: cover;
	transition: transform 0.6s ease-in-out;
	transform: translateX(0);
}

.overlay-panel {
	height: 100%;
	width: 50%;
	position: absolute;
	top: 0;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	transform: translateX(0);
	transition: transform 0.6s ease-in-out;
}

.overlay-left {
	transform: translateX(-20%);
}

.overlay-right {
	right: 0;
	transform: translateX(0);
}

/* 设计激活时叠加层的位置 */
.panel-active .overlay-left {
	transform: translateX(0);
}

.panel-active .container-overlay {
	transform: translateX(-100%);
}

.panel-active .overlay {
	transform: translateX(50%);
}

/* 设置激活时，登录注册层的位置和透明度 */
.panel-active .container-signIn {
	transform: translateX(100%);
}

.panel-active .container-signup {
	opacity: 1;
	z-index: 5;
	transform: translateX(100%);
}


* {
	transition: 1s;
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}

.login-app {
	position: absolute;
	top: 40px; /* 调整值以使文本垂直对齐到底部 */
	width: 375px;
	max-height: 420px;
	margin-bottom: 400px;
	background-color: #fdfdfd;
	border-radius: 12px 12px 20px 20px;
	/* 避免空格换行缝隙影响布局 */
	font-size: 0;
}

input[type="radio"] {
	/* 单选框要默认隐藏 */
	display: none;
}

label.u-btn {
	display: inline-block;
	background-color: #f5f5f6;
	font-size: 20px;
	color: rgba(37, 38, 43, .36);
	padding: 20px;
	text-align: center;
	width: 50%;
	cursor: pointer;
}


#uname:checked + .u-btn {
	background-color: #fdfdfd;
	color: #25262b;
	cursor: default;
}


#uname:checked ~ #form-bar {
	transform: translateX(0px);
}

iframe {
	border: none;
}

#form-bar {
	display: flex;
	text-align: end;
	transition: transform .6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
	font-size: 16px;
}

form {
	flex-shrink: 30;
	width: 160%;
	padding: 36px;
	position: relative;
}

.card .front p {
	font-size: 15px;
	position: absolute;
	width: 400px;
	top: 55%;
	left: 50%;
	transform: translateX(-50%) translateY(-50%) translateZ(60px);
	backface-visibility: hidden;
	color: #007afd;
}

.card .back .hero h2 {
	color: #f8fae4;
	font-weight: 400;
	margin-bottom: 10px;
}

.card .back span {
	display: inline-block;
	width: 40px;
	height: 25px;
	text-align: end;
	background-color: #c09f5a;
	line-height: 25px;
	border-radius: 2px;
	color: #191d20;
	font-weight: bold;
}

* {
	user-select: none;
}

.login_Image {
	max-width: 750px; /* 设置最大宽度为300像素 */
	max-height: 400px; /* 设置最大高度为200像素 */
	min-width: 750px;
	min-height: 400px;
}
</style>

